package nix.anim.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import nix.anim.common.pojo.NixResult;
import nix.anim.pojo.Menu;
import nix.anim.pojo.Right;
import nix.anim.pojo.Role;
import nix.anim.pojo.User;
import nix.anim.service.MenuService;
import nix.anim.service.RightService;
import nix.anim.service.RoleRightService;
import nix.anim.service.RoleService;
import nix.anim.service.UserRoleService;
import nix.anim.service.UserService;

/**
 * 页面展示Controller
 */
@Controller
public class PageController {

	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RightService rightService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private RoleRightService roleRightService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private UserRoleService userRoleService;
	
	@RequestMapping("/")
	public String showIndex(Model model, HttpServletRequest request, HttpServletResponse response) {
		// 获取当前用户
		User user = (User) request.getAttribute("currentUser");
		model.addAttribute("currentUser", user);
		return "index";
	}
	
	@RequestMapping("/home")
	public String showHome() {
		return "home";
	}
	
	@RequestMapping("/user")
	public String showUser() {
		return "user";
	}
	
	@RequestMapping("/role")
	public String showRole() {
		return "role";
	}
	
	@RequestMapping("/system/init.html")
	public String sysInit() {
		// 菜单
		List<Menu> menuList = new ArrayList<>();
		menuList.add(new Menu(1, 0, "用户和权限", true, "home", "", "el-icon-edit", 1, 1));
		menuList.add(new Menu(2, 1, "用户管理", false, "home", "/user", "", 1, 1));
		menuList.add(new Menu(3, 1, "角色管理", false, "home", "/role", "", 2, 1));
		
		menuList.add(new Menu(4, 0, "日志管理", true, "home", "", "el-icon-tickets", 2, 1));
		menuList.add(new Menu(5, 4, "登录日志", false, "home", "/log-login", "", 1, 1));
		menuList.add(new Menu(6, 4, "操作日志", false, "home", "/log-operation", "", 2, 1));
		
		menuList.add(new Menu(7, 0, "内容管理", true, "cms", "", "el-icon-news", 2, 1));
		menuList.add(new Menu(8, 7, "站点管理", false, "cms", "/cms-site", "", 1, 1));
		menuList.add(new Menu(9, 7, "栏目管理", false, "cms", "/cms-cate", "", 2, 1));
		menuList.add(new Menu(10, 7, "内容管理", false, "cms", "/cms-content", "", 3, 1));
		menuService.deleteAll();
		for (Menu menu : menuList) {
			menuService.addMenu(menu);
		}
		// 权限
		// 权限类型：1、查看；2、新增；3、修改；4、删除；11、重置密码；12、指定角色；13、指定权限；
		List<Right> rightList = new ArrayList<>();
		/*用户和权限*/
		rightList.add(new Right(101, 1, 1, "查看"));
		/*用户管理*/
		rightList.add(new Right(201, 2, 1, "查看"));
		rightList.add(new Right(202, 2, 2, "新增"));
		rightList.add(new Right(203, 2, 3, "修改"));
		rightList.add(new Right(211, 2, 11, "重置密码"));
		rightList.add(new Right(212, 2, 12, "指定角色"));
		/*角色管理*/
		rightList.add(new Right(301, 3, 1, "查看"));
		rightList.add(new Right(302, 3, 2, "新增"));
		rightList.add(new Right(303, 3, 3, "修改"));
		rightList.add(new Right(313, 3, 13, "指定权限"));
		/*日志管理*/
		rightList.add(new Right(401, 4, 1, "查看"));
		/*登录日志*/
		rightList.add(new Right(501, 5, 1, "查看"));
		/*操作日志*/
		rightList.add(new Right(601, 6, 1, "查看"));
		/*内容管理*/
		rightList.add(new Right(701, 7, 1, "查看"));
		/*站点管理*/
		rightList.add(new Right(801, 8, 1, "查看"));
		rightList.add(new Right(802, 8, 2, "新增"));
		rightList.add(new Right(803, 8, 3, "修改"));
		/*栏目管理*/
		rightList.add(new Right(901, 9, 1, "查看"));
		rightList.add(new Right(902, 9, 2, "新增"));
		rightList.add(new Right(903, 9, 3, "修改"));
		/*文章管理*/
		rightList.add(new Right(1001, 10, 1, "查看"));
		rightList.add(new Right(1002, 10, 2, "新增"));
		rightList.add(new Right(1003, 10, 3, "修改"));
		rightService.deleteAll();
		for (Right right : rightList) {
			rightService.addRight(right);
		}
		// 角色
		roleService.deleteAll();
		NixResult resultRole = roleService.addRole("超级用户", "超级用户角色");
		Role role = (Role) resultRole.getData();
		// System.out.println(role);
		// 角色权限
		roleRightService.deleteAll();
		roleRightService.addRoleRight(role.getId(), 101);
		
		roleRightService.addRoleRight(role.getId(), 201);
		roleRightService.addRoleRight(role.getId(), 202);
		roleRightService.addRoleRight(role.getId(), 203);
		roleRightService.addRoleRight(role.getId(), 211);
		roleRightService.addRoleRight(role.getId(), 212);
		
		roleRightService.addRoleRight(role.getId(), 301);
		roleRightService.addRoleRight(role.getId(), 302);
		roleRightService.addRoleRight(role.getId(), 303);
		roleRightService.addRoleRight(role.getId(), 313);

		roleRightService.addRoleRight(role.getId(), 401);

		roleRightService.addRoleRight(role.getId(), 501);

		roleRightService.addRoleRight(role.getId(), 601);

		roleRightService.addRoleRight(role.getId(), 701);
		
		roleRightService.addRoleRight(role.getId(), 801);
		roleRightService.addRoleRight(role.getId(), 802);
		roleRightService.addRoleRight(role.getId(), 803);
		
		roleRightService.addRoleRight(role.getId(), 901);
		roleRightService.addRoleRight(role.getId(), 902);
		roleRightService.addRoleRight(role.getId(), 903);
		
		roleRightService.addRoleRight(role.getId(), 1001);
		roleRightService.addRoleRight(role.getId(), 1002);
		roleRightService.addRoleRight(role.getId(), 1003);
		// 用户
		userService.deleteAll();
		NixResult resultUser = userService.addUser(new User("admin", "race", "13772194782", "13772194782@qq.com", 1));
		User user = (User) resultUser.getData();
		// 用户角色
		userRoleService.deleteAll();
		userRoleService.addUserRole(user.getId(), role.getId());
		// 返回逻辑视图		
		return "system/init-success";
	}
}
